const modules = import.meta.glob('@/views/modules/**/*.vue');

// 懒加载
function loadView(view: string) {
  const path = `/src/views/modules/${view}.vue`;
  const load = modules[path];
  if (!load) {
    console.error(`未找到视图文件: ${path}`);
  }
  return load;
}

// 将后端返回的路由转成前端可用
export function transformBackendRoutes(routes: any[]): any[] {
  return routes.map((route) => {
    const temp: any = {
      path: route.path,
      name: route.name,
      meta: route.meta || {},
      hidden: route.hidden,
      redirect: route.redirect || undefined,
      children: [],
    };

    // 处理组件映射
    if (route.component === 'Layout') {
      temp.component = null;
    } else if (route.component === 'ParentView') {
      temp.component = () => import('@/components/parent-view/index.vue');
    } else if (route.component) {
      temp.component = loadView(route.component);
    }

    // 递归 children
    if (route.children && route.children.length) {
      temp.children = transformBackendRoutes(route.children);
    }

    return temp;
  });
}
